package com.yhn.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author 岳浩楠
 * @Date 2024-05-12 20:17
 * @Version 1.0
 * 该类作用与解析properties配置文件
 */
public class PropertiesUtils {

    private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);

    private static Map<String, Object> propertiesMap = new ConcurrentHashMap();


    /**
     *  读取properties配置文件
     */
    static {
        InputStream is = null;

        is = PropertiesUtils.class.getClassLoader().getResourceAsStream("application.properties");

        Properties properties = new Properties();
        try {
            properties.load(is);
            /**
             *  遍历properties配置文件 得到当前的配置文件中的所有的key
             */
            Iterator<Object> iterator = properties.keySet().iterator();
            while (iterator.hasNext()) {
                String propertiesName = (String) iterator.next();
                String propertiesValue = properties.getProperty(propertiesName);
                //logger.info("propertiesName = {}", propertiesName);
                //logger.info("propertiesValue = {}", propertiesValue);
                /**
                 * 封装进map集合
                 */
                propertiesMap.put(propertiesName, propertiesValue);
            }
        } catch (IOException e) {
            logger.error("读取properties文件失败");
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    logger.error("InputStream流关闭失败");
                }
            }
        }

    }

    public static Object getProperties(String key) {
        return propertiesMap.get(key);
    }


}
